﻿Imports Microsoft.ResourceManagement.Workflow.Activities
Imports Microsoft.IdentityManagement.WebUI.Controls
Imports System.Web.UI.WebControls

Namespace FIMUIs
    Public Class MakeAccountNameUI
        Inherits ActivitySettingsPart

        'Private _uicore As UIUtils

        'Private ReadOnly Property UICore() As UIUtils
        '    Get
        '        If Me._uicore Is Nothing Then
        '            Me._uicore = New UIUtils()
        '        End If
        '        Return Me._uicore
        '    End Get
        'End Property


        'Public UIUtils As UIUtils = New UIUtils

        Public Overrides Function GenerateActivityOnWorkflow(workflow As SequentialWorkflow) As Activity

            If Not Me.ValidateInputs() Then
                Return Nothing
            End If
            Dim MakeAccountNameActivity As New MakeAccountName

            MakeAccountNameActivity.ActivityEnabled = Me.getChecked("chkEnabled")
            MakeAccountNameActivity.stdName = Me.GetText("txtStdName")
            MakeAccountNameActivity.uniqueName = Me.GetText("txtUniqueName")
            MakeAccountNameActivity.exName = Me.GetText("txtExName")
            MakeAccountNameActivity.exSearch = Me.GetText("txtExSearch")

            Return MakeAccountNameActivity

        End Function

        Public Overrides Sub LoadActivitySettings(activity As Activity)

            Dim MakeAccountNameActivity As MakeAccountName = TryCast(activity, MakeAccountName)
            If MakeAccountNameActivity IsNot Nothing Then
                Me.setChecked("chkEnabled", MakeAccountNameActivity.ActivityEnabled)
                Me.SetText("txtStdName", MakeAccountNameActivity.stdName)
                Me.SetText("txtUniqueName", MakeAccountNameActivity.uniqueName)
                Me.SetText("txtExName", MakeAccountNameActivity.exName)
                Me.SetText("txtExSearch", MakeAccountNameActivity.exSearch)
            End If

        End Sub

        Public Overrides Function PersistSettings() As ActivitySettingsPartData

            Dim data As New ActivitySettingsPartData()

            data("ActivityEnabled") = Me.getChecked("chkEnabled")
            data("stdName") = Me.GetText("txtStdName")
            data("uniqueName") = Me.GetText("txtUniqueName")
            data("exName") = Me.GetText("txtExName")
            data("exSearch") = Me.GetText("txtExSearch")

            Return data

        End Function

        Public Overrides Sub RestoreSettings(data As ActivitySettingsPartData)

            Me.setChecked("chkEnabled", DirectCast(data("ActivityEnabled"), Boolean))
            Me.SetText("txtStdName", DirectCast(data("stdName"), String))
            Me.SetText("txtUniqueName", DirectCast(data("uniqueName"), String))
            Me.SetText("txtExName", DirectCast(data("exName"), String))
            Me.SetText("txtExSearch", DirectCast(data("exSearch"), String))

        End Sub

        Public Overrides Sub SwitchMode(mode As ActivitySettingsPartMode)

        End Sub

        Public Overrides ReadOnly Property Title As String

            Get
                Return "Create Unique Account Name for a User"
            End Get

        End Property

        Public Overrides Function ValidateInputs() As Boolean

            Return True

        End Function

        Protected Overrides Sub CreateChildControls()

            Dim controlLayoutTable As Table
            controlLayoutTable = New Table()

            'Width is set to 100% of the control size
            controlLayoutTable.Width = Unit.Percentage(100.0)
            controlLayoutTable.BorderWidth = 1
            controlLayoutTable.CellPadding = 2

            controlLayoutTable.Rows.Add(Me.AddNote("Instructions:", "Enter the attribute names (case dependent) and any separators " & _
                                                  "(e.g. periods) joined by ampersands (&). If Substrings are required, the length of the substring " & _
                                                  "should be included in brackets at the end of the attribute in question."))
            controlLayoutTable.Rows.Add(Me.AddDivider)
            controlLayoutTable.Rows.Add(Me.AddCheckBox("Is Function Enabled:", "chkEnabled", False))
            controlLayoutTable.Rows.Add(Me.AddDivider)
            controlLayoutTable.Rows.Add(Me.AddNote("Standard Pattern:", "This is the pattern to be applied to all users apart " & _
                                                   "from exceptions (see below). E.g. givenName(1)&.&sn for j.smith"))
            controlLayoutTable.Rows.Add(Me.AddTableRowTextBox("Standard Name Pattern:", "txtStdName", 400, 100, False, "e.g. givenName(1)&.&sn"))
            controlLayoutTable.Rows.Add(Me.AddDivider)
            controlLayoutTable.Rows.Add(Me.AddNote("Uniqueness Pattern:", "This is the pattern to be applied if the standard name " & _
                                                   "is in use. The code will look for the hash symbol '#' and assume it should increment " & _
                                                   "at that point until uniqueness is achieved. E.g. givenName(1)&.&sn&.&# for j.smith.1, j.smith.99 etc"))
            controlLayoutTable.Rows.Add(Me.AddTableRowTextBox("Unique Name Pattern:", "txtUniqueName", 400, 100, False, "e.g. givenName(1).sn.1"))
            controlLayoutTable.Rows.Add(Me.AddDivider)
            controlLayoutTable.Rows.Add(Me.AddNote("Exception Pattern:", "This is the pattern for exceptions. The Exception Search Criteria must be " & _
                                                   "an XPATH filter for the search e.g. /Person[JobTitle = Director]"))
            controlLayoutTable.Rows.Add(Me.AddTableRowTextBox("Exception Name Pattern:", "txtExName", 400, 100, False, "e.g. givenName&.&sn"))
            controlLayoutTable.Rows.Add(Me.AddTableRowTextBox("Exception Search Criteria:", "txtExSearch", 400, 100, False, "e.g. /Person[JobTitle = Director]"))
            'controlLayoutTable.Rows.Add(Me.AddDivider)

            'controlLayoutTable.Rows.Add(Me.UICore.AddCheckBox("Is Mail Function Enabled:", "chkEnabled", False))

            Me.Controls.Add(controlLayoutTable)

            MyBase.CreateChildControls()

        End Sub

#Region "Utility Functions"

        Private Function CreateCheckBox(ByVal label As String, ByVal ID As String, ByVal defaultValue As Boolean)

            Dim row As New TableRow
            Dim cell1 As New TableCell
            Dim cell2 As New TableCell
            Dim label1 As New Label
            Dim checkbox As New CheckBox

            'cell1
            label1.Text = label
            label1.CssClass = MyBase.LabelCssClass
            cell1.Controls.Add(label1)

            'cell2
            checkbox.ID = ID
            checkbox.CssClass = MyBase.TextBoxCssClass
            checkbox.Checked = defaultValue
            cell2.Controls.Add(checkbox)

            row.Controls.Add(cell1)
            row.Controls.Add(cell2)

            Return row

        End Function

        'create a tablerow that contains a label and textbox
        Private Function AddTableRowTextBox(ByVal labelText As [String], ByVal controlID As [String], ByVal width _
                                            As Integer, ByVal maxLength As Integer, ByVal multiLine As [Boolean], _
                                            ByVal defaultValue As [String]) As TableRow

            Dim row As New TableRow()
            Dim labelCell As New TableCell()
            Dim controlCell As New TableCell()
            Dim oLabel As New Label()
            Dim oText As New TextBox()

            oLabel.Text = labelText
            oLabel.CssClass = MyBase.LabelCssClass
            labelCell.Controls.Add(oLabel)
            oText.ID = controlID
            oText.CssClass = MyBase.TextBoxCssClass
            oText.MaxLength = maxLength
            oText.Width = width
            'oText.Width = width
            oText.Text = defaultValue

            If multiLine Then
                oText.TextMode = TextBoxMode.MultiLine
                oText.Rows = System.Math.Min(6, (maxLength + 60) \ 60)
                oText.Wrap = True
            End If

            controlCell.Controls.Add(oText)
            row.Cells.Add(labelCell)
            row.Cells.Add(controlCell)

            Return row

        End Function

        'Adds a row with a simple label and text
        Private Function AddTableRowLabel(ByVal text As String)

            Dim row As New TableRow()
            Dim labelCell As New TableCell()
            Dim oLabel As New Label()

            oLabel.Text = text
            oLabel.CssClass = MyBase.LabelCssClass
            oLabel.Width = Unit.Pixel(400)
            labelCell.Controls.Add(oLabel)
            row.Cells.Add(labelCell)

            Return row

        End Function

        Private Function AddNote(ByVal notetitle As String, ByVal noteText As String)

            Dim row As New TableRow()
            Dim cell1 As New TableCell()
            Dim cell2 As New TableCell()
            Dim label1 As New Label()
            Dim label2 As New Label()

            'label1
            label1.Text = notetitle
            label1.CssClass = MyBase.LabelCssClass
            label1.Width = 100
            cell1.Controls.Add(label1)

            'label2
            label2.Text = noteText
            label2.CssClass = MyBase.LabelCssClass
            cell2.Controls.Add(label2)

            row.Cells.Add(cell1)
            row.Cells.Add(cell2)

            Return row

        End Function

        Private Function AddDivider()

            Dim dividerRow As New TableRow()
            Dim dividerCell As New TableCell()
            dividerCell.ColumnSpan = 2
            Dim divider As New Literal
            divider.Text = "<hr/>"
            dividerCell.Controls.Add(divider)
            dividerRow.Controls.Add(dividerCell)

            Return dividerRow

        End Function

        Private Function AddCheckBox(ByVal label As String, ByVal id As String, ByVal defaultValue As Boolean)

            Dim row As New TableRow()
            Dim labelCell As New TableCell()
            Dim checkboxCell As New TableCell()
            Dim label1 As New Label()
            Dim checkbox1 As New CheckBox()

            label1.Text = label
            label1.CssClass = MyBase.LabelCssClass
            labelCell.Controls.Add(label1)

            checkbox1.ID = id
            checkbox1.CssClass = MyBase.TextBoxCssClass
            checkbox1.Checked = defaultValue
            checkboxCell.Controls.Add(checkbox1)

            row.Cells.Add(labelCell)
            row.Cells.Add(checkboxCell)

            Return row

        End Function

        Private Function GetText(ByVal textBoxID As String) As String

            Dim textBox As TextBox = DirectCast(Me.FindControl(textBoxID), TextBox)
            Return If(textBox.Text, [String].Empty)

        End Function

        Private Sub SetText(ByVal textBoxID As String, ByVal text As String)

            Dim textBox As TextBox = DirectCast(Me.FindControl(textBoxID), TextBox)
            If textBox IsNot Nothing Then
                textBox.Text = text
            Else
                textBox.Text = ""
            End If

        End Sub

        Private Function getChecked(ByVal checkBoxID As String)

            Dim checkBox As CheckBox = DirectCast(Me.FindControl(checkBoxID), CheckBox)
            Return checkBox.Checked

        End Function

        Private Sub setChecked(ByVal checkBoxID As String, ByVal value As Boolean)

            Dim checkBox As CheckBox = DirectCast(Me.FindControl(checkBoxID), CheckBox)
            checkBox.Checked = value

        End Sub

        'Set the text box to read mode or read/write mode 
        Private Sub SetTextBoxReadOnlyOption(ByVal textBoxID As String, ByVal [readOnly] As Boolean)

            Dim textBox As TextBox = DirectCast(Me.FindControl(textBoxID), TextBox)
            textBox.ReadOnly = [readOnly]
            'textBox.Enabled = (Not [readOnly])

        End Sub



#End Region

    End Class


End Namespace



